<script setup>
import {reactive, ref, computed} from "vue";
// 引入添加客资的组件
import PublicDetail from "./components/public-detail.vue";
import {acquisitionApi} from "@/service/member";

// 导入分页组件
import CommonPagination from "@/components/pagination.vue";

const pagination = reactive({
  page: 1,
  limit: 15,
  total: 0,
})

// 导入客资接口
import {getPublicListApi} from "@/service/member";

// 设置检索form
const filterForm = ref({
  keyword: "",
});

// 获取客资列表

const contact = ref('');
const fetchList = async () => {
  let params = {
    ...filterForm.value,
    pagination: pagination
  }
  const {data} = await getPublicListApi(params);
  contact.value = data.contact;
  list.value = data.list;
  pagination.total = data.pagination.total;
};

// 初始化客资列表
const list = ref([]);
// 搜索客资
const handleSearch = () => {
  pagination.page = 1;
  fetchList();
};

// 查看客资详情
const publicDetailRef = ref(null);
const showDetailDrawer = (row) => {
  publicDetailRef.value.openDrawer(row);
};

// 获取客资
const doCatch = () => {
  acquisitionApi({contact:contact.value}).then(({code, msg}) => {
    if (code === 200) {
      ElMessage.success(msg);
    } else {
      ElMessage.error(msg);
    }
  });
};

onMounted(() => {
  fetchList();
});
</script>
<template>
  <el-main>
    <div class="toolbar">
      <el-input v-model="filterForm.keyword" placeholder="姓名/联系方式" @keyup.enter="handleSearch"/>
      <el-button type="primary" icon="search" @click="handleSearch">搜索</el-button>
    </div>

    <el-table class="list" :data="list" border>
      <el-table-column label="操作" width="200" align="center" fixed="left">
        <template #default="{row}">
          <el-button type="primary" size="small" @click="showDetailDrawer(row)">查看</el-button>
          <el-button type="danger" size="small" v-if="'' !== contact" @click="doCatch(row)">获取</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="visit_times" label="回访次数" width="100" align="center"/>
      <el-table-column prop="visit_status.title" label="意向" width="100" align="center"/>
      <el-table-column prop="category.title" label="信息类型" align="center"/>
      <el-table-column prop="contact.name" label="姓名"/>
      <el-table-column prop="contact.contact" label="联系方式"/>
      <el-table-column prop="city.name" label="城市"/>
      <el-table-column prop="product.event_date" label="婚期"/>
      <el-table-column prop="product.budget" label="预算"/>
      <el-table-column prop="source.title" label="渠道"/>
      <el-table-column prop="create_time" label="分配时间"/>
    </el-table>

    <div class="pagination">
      <common-pagination :pagination="pagination" @fetch-list="fetchList"/>
    </div>
    <public-detail ref="publicDetailRef"/>
  </el-main>
</template>
